//
//  ProductFirstCell.m
//  Ufan
//
//  Created by yangqingluo on 15/12/6.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "ProductFirstCell.h"
#import "UIResponder+Router.h"

@interface ProductFirstCell()

@property (strong, nonatomic) UILabel *ownerLabel;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UILabel *introLabel;

@end

@implementation ProductFirstCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:20.0];
        self.textLabel.numberOfLines = 0;
        self.textLabel.textColor = [UIColor blackColor];
        
        self.detailTextLabel.numberOfLines = 0;
        self.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 16)];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_timeLabel];
        
        _introLabel = [[UILabel alloc] init];
        _introLabel.font = [UIFont systemFontOfSize:14];
        _introLabel.textAlignment = NSTextAlignmentLeft;
        _introLabel.backgroundColor = [UIColor clearColor];
        _introLabel.textColor = RGBA(105, 105, 105, 1);
        _introLabel.numberOfLines = 0;
        [self.contentView addSubview:_introLabel];
        
        _ownerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 115, 16)];
        _ownerLabel.font = [UIFont systemFontOfSize:12];
        _ownerLabel.textAlignment = NSTextAlignmentLeft;
        _ownerLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_ownerLabel];
        
        
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 16)];
        _priceLabel.font = [UIFont systemFontOfSize:18];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        _priceLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:_priceLabel];
        
        
        [self.contentView addSubview:self.detailButton];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.textLabel.frame = CGRectMake(20, 15, self.width - 40, 50);
    self.textLabel.text = self.info.name;
    
    self.priceLabel.frame = CGRectMake(self.textLabel.left, self.textLabel.bottom + 10, self.priceLabel.width, self.priceLabel.height);
    self.priceLabel.text = self.info.price;
    
    self.timeLabel.frame = CGRectMake(self.textLabel.left, self.priceLabel.bottom + 10, self.timeLabel.width, self.timeLabel.height);
    self.timeLabel.text = [NSString stringWithFormat:@"剩余%d份 已售%d份",self.info.inventory,self.info.saledNum];
    
    self.detailButton.center = CGPointMake(0.5 * self.width, self.height - _detailButton.height);
    
    if (self.info.intro) {
        self.introLabel.frame = CGRectMake(self.textLabel.left, self.timeLabel.bottom, self.textLabel.width, self.detailButton.top - self.timeLabel.bottom);
        self.introLabel.text = self.info.intro;
    }
}

+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 240;
}

- (void)detailButtonClicked{
    [self routerEventWithName:UF_Router_Event_ProductDetailButton userInfo:nil];
}

#pragma getter
- (UIButton *)detailButton{
    if (!_detailButton) {
        _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailButton.frame = CGRectMake(0, 0, 200, 30);
        _detailButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_detailButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        [_detailButton setTitle:@"查看详情〉" forState:UIControlStateNormal];
        [_detailButton addTarget:self action:@selector(detailButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _detailButton;
}

@end
